package com.seagull.sb.service;

import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.seagull.sb.bo.NoticeBO;
import com.seagull.sb.domain.Notice;
import com.seagull.sb.domain.NoticeSubject;
import com.seagull.sb.domain.Student;
import com.seagull.sb.dto.NoticeDto;
import com.seagull.sb.util.SBRequestContext;
import com.seagull.sb.util.ServiceMapper;

@Component
@Path("/notice")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class NoticeService extends BaseService {

	@Autowired 
	NoticeBO noticeBO;
	
	private ServiceMapper sm = ServiceMapper.getInstaMapper();

	@GET
	@Path("/")
	@RolesAllowed({"student","teacher"})
	public List<NoticeDto> getNotice(@Context SecurityContext sc) {
		SBRequestContext rc = populateRequestContext(sc); 
		Student student = rc.getStudent();
		
		List<Notice> notices = noticeBO.getNotice(rc,new NoticeDto());
		return sm.mapToNoticeDto(notices);

	}
	
	@GET
	@Path("/subjects")
	@RolesAllowed({"teacher"})
	public List<NoticeSubject> getNoticeSubjects(@Context SecurityContext sc) {
		SBRequestContext rc = populateRequestContext(sc); 
		return noticeBO.getNoticeSubjects(rc);
	}
	
	@GET
	@Path("/{classid}")
	@RolesAllowed({"teacher"})
	public List<NoticeDto> getNoticeByClass(@Context SecurityContext sc,
			@PathParam("classid") int classid) {
		SBRequestContext rc = populateRequestContext(sc); 
		List<Notice> notices = noticeBO.getNotice(rc,new NoticeDto());
		return sm.mapToNoticeDto(notices);

	}
	
	@GET
	@Path("/view/{noticeid}")
	@RolesAllowed({"student","teacher"})
	public List<NoticeDto> viewNotice(@Context SecurityContext sc, 
			@PathParam("noticeid") int noticeid) {
		SBRequestContext rc = populateRequestContext(sc); 

		NoticeDto noticeDto  = new NoticeDto();
		noticeDto.setId(noticeid);

		List<Notice> notices = noticeBO.viewedNotice(rc,noticeDto);
		return sm.mapToNoticeDto(notices);

	}
 
	@POST
	@Path("/")
	public NoticeDto saveNotice(@Context SecurityContext sc, 
			NoticeDto noticeDto) {
		SBRequestContext rc = new SBRequestContext();
		Notice notice = sm.mapToNotice(noticeDto);
		noticeBO.saveNotice(rc, notice);
		System.out.println("saveNotice - Successful.");
		return noticeDto;
 	}
}